/**
 * @description 路由拦截状态管理，目前两种模式：all模式与intelligence模式，其中partialRoutes是菜单暂未使用
 */
import AccountApi from "/@/api/AccountApi";
import router, {addRouter, constantRoutes} from "/@/router";
import {
    convertRouter,

} from "/@/utils/routes";
import {defineStore} from "pinia";
import {setLocalStorageCache} from "/@/utils/localStorage";
import {getLocalStorageCache} from "/@/utils/localStorage";
import treeUtil from "/@/utils/treeUtil.ts";


const filterBreadcrumb = (data: any) => {
    return data.reduce((acc: any, item: any) => {
        const newItem = {...item};
        if (item.children && item.children.length > 0)
            newItem.children = filterBreadcrumb(item.children);
        return [...acc, newItem];
    }, []);
};

export const useRoutesStore = defineStore("routes", {
    state: (): RoutesModuleType => ({
        tab: {
            data: undefined,
        },
        tabMenu: undefined,
        activeMenu: {
            data: undefined,
        },
        routes: [],
        allRoutes: [],
        breadcrumbRoutes: [],
    }),
    getters: {
        getTab: (state) => state.tab,
        getTabMenu: (state) =>
        state.tab.data
                ? state.routes.find((route) => {
                // console.log(route.name);
                return route.name === state.tab.data
            })
                : {meta: {title: ""}},
        getActiveMenu: (state) => state.activeMenu,
        getRoutes: (state) =>
            state.routes.filter(
                (_route) => _route.meta && _route.meta.hidden !== true
            ),
        getAllRoutes: (state) =>
            state.allRoutes.filter(
                (_route) => _route.meta && _route.meta.hidden !== true
            ),
        getBreadcrumbRoutes: (state) =>
            state.breadcrumbRoutes.filter(
                (_route) => _route.meta && _route.meta.hidden !== true
            ),
        getPartialRoutes: (state) =>
            state.tab.data
                ? state.routes.find((route) => route.name === state.tab.data) &&
                state.routes.find((route) => route.name === state.tab.data).children
                : []
    },
    actions: {
        /**
         * @description 多模式设置路由
         * @param mode
         * @returns
         */
        async setRoutes(mode = "none") {
            try{
                const {data} = await AccountApi.getMenu();
                let tree = treeUtil.treeData(data)
                convertRouter(tree);
                console.log(tree);
                tree.sort((a, b) => a.sort - b.sort)
                let accessRoutes = [...constantRoutes, ...tree]
                this.routes = accessRoutes
                this.allRoutes = accessRoutes;
                this.breadcrumbRoutes = filterBreadcrumb(accessRoutes);
                console.log(accessRoutes);
                addRouter(accessRoutes);
                return true;
            }catch{
                return false;
            }
        },
        changeMenuMeta(options: any) {
            function handleRoutes(routes: any[]) {
                return routes.map((route) => {
                    if (route.name === options.name)
                        Object.assign(route.meta, options.meta);
                    if (route.children && route.children.length > 0)
                        route.children = handleRoutes(route.children);
                    return route;
                });
            }

            this.routes = handleRoutes(this.routes);
        },
        /**
         * @description 修改 activeName
         * @param activeMenu 当前激活菜单
         */
        changeActiveMenu(activeMenu: string) {
            this.activeMenu.data = activeMenu;
        },
    },
});
